<?php
/**
 * @property int $id
 * @property string $title
 * @property string $description
 * @property string $content
 * @property string $created_at
 * @property string $category_id
 */
class Article extends ActiveRecord
{
	public static function model($className = __CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return 'article';
	}

	public function attributeLabels()
	{
		return array(
			'title' => 'Заголовок',
			'description' => 'Описание',
			'content' => 'Содержимое',
			'created_at' => 'Дата',
			'category_id' => 'Категория',
			'country_id' => 'Страна',
		);
	}

	public function relations()
	{
		return array(
			'articleToTag' => array(self::HAS_MANY, 'ArticleToTag', 'article_id'),
			'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
			'country' => array(self::BELONGS_TO, 'Country', 'country_id'),
		);
	}
	
	public function rules()
	{
		return array(
			array('title, description, content, created_at, category_id', 'required'),
			array('id, title, description, content, created_at, category_id', 'safe'),
		);
	}
	
	public function search()
	{
		$criteria = new CDbCriteria;
		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title, true);
		if (!empty ($this->created_at)) {
			$criteria->compare('created_at', Yii::app()->format->dateHumanToNormal($this->created_at));
		}
		$criteria->compare('category_id',$this->category_id);
		return new CActiveDataProvider('Article', array(
			'criteria'=>$criteria,
			'sort'=>array(
				'defaultOrder'=>'id DESC',
			),
		));
	}
}